package api

import (
	"github.com/gin-gonic/gin"
	"main/app"
	"main/internal/model"
	"main/internal/pkg/code"
	"main/internal/service"
	"net/http"
)

func init() {
	c := &SysGiftApi{}
	c.init(app.R) //这里需要引入你的gin框架的实例
}

// SysGiftApi 控制器
type SysGiftApi struct {
	Page     int    `form:"page,default=1"`
	Size     int    `form:"size,default=10"`
	Kw       string `form:"kw"`
	IDS      []uint `form:"ids"`
	ID       uint   `form:"id"`
	DateForm string `form:"date_form"`
	DateTo   string `form:"date_to"`
	Status   string `form:"status"`
	Lang     string `form:"lang"`
}

func (s SysGiftApi) init(g *gin.Engine) {
	// 依次: 分页列表，单条，插入，修改，删除
	group := g.Group("/api/Gift")
	//新增
	group.POST("/Create", s.create)
	//批量新增
	group.POST("/CreateList", s.createList)
	//根据ID删除
	group.DELETE("/Delete", s.delete)
	//根据ID批量删除
	group.DELETE("/DeleteList", s.deleteList)
	//根据ID修改
	group.PUT("/Update", s.update)
	//根据ID查询
	group.GET("/Get", s.get)
	//批量查询 分页、关键字、ID列表、时间、状态
	group.GET("/GetList", s.getList)

	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/Create", ApiMethod: "POST", ApiDescribe: "新增", ApiGroup: "Gift"})
	//service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/SysGiftApi/Login", ApiMethod: "POST", ApiDescribe: "登录", ApiGroup: "SysGiftApi"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/CreateList", ApiMethod: "POST", ApiDescribe: "批量新增", ApiGroup: "Gift"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/Delete", ApiMethod: "DELETE", ApiDescribe: "根据ID删除", ApiGroup: "Gift"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/DeleteList", ApiMethod: "DELETE", ApiDescribe: "根据ID批量删除", ApiGroup: "Gift"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/Update", ApiMethod: "PUT", ApiDescribe: "根据ID修改", ApiGroup: "Gift"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/Get", ApiMethod: "GET", ApiDescribe: "根据ID查询", ApiGroup: "Gift"})
	service.SysApiListService.Create(model.SysApiList{ApiPath: "/api/Gift/GetList", ApiMethod: "GET", ApiDescribe: "批量查询", ApiGroup: "Gift"})
}

// create 新增
func (s SysGiftApi) create(c *gin.Context) {
	var v model.SysGift
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	create, err := service.SysGiftService.Create(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBInsertError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(create, code.GetMsg(code.DBInsertSuccess, s.Lang)))
	}
}

// createList 批量新增
func (s SysGiftApi) createList(c *gin.Context) {
	var v []model.SysGift
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	create, err := service.SysGiftService.CreateList(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBInsertError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(create, code.GetMsg(code.DBInsertSuccess, s.Lang)))
	}
}

// delete 删除
func (s SysGiftApi) delete(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	uid := c.GetUint("user_id")
	deleteDate, err := service.SysGiftService.Delete(s.ID, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBDeleteError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(deleteDate, code.GetMsg(code.DBDeleteSuccess, s.Lang)))
	}
}

// deleteList 根据ID批量删除
func (s SysGiftApi) deleteList(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	uid := c.GetUint("user_id")
	deleteDate, err := service.SysGiftService.DeleteList(s.IDS, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBDeleteError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(deleteDate, code.GetMsg(code.DBDeleteSuccess, s.Lang)))
	}
}

// get 根据ID查询
func (s SysGiftApi) get(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	getDate, err := service.SysGiftService.Get(s.ID, s.Lang)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang)))
	}
}

// getList 批量查询
func (s SysGiftApi) getList(c *gin.Context) {
	_ = c.ShouldBindQuery(&s)
	getDate, err, count := service.SysGiftService.GetList(s.Page, s.Size, s.Kw, s.IDS, s.ID, s.DateForm, s.DateTo, s.Status, s.Lang)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkDataList(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang), count))
	}
}

// update 根据ID更新数据
func (s SysGiftApi) update(c *gin.Context) {
	var v model.SysGift
	_ = c.ShouldBindQuery(&s)
	_ = c.ShouldBindJSON(&v)
	uid := c.GetUint("user_id")
	getDate, err := service.SysGiftService.UpdateData(v, uid)
	if err != nil {
		c.JSON(http.StatusOK, model.Fail(code.GetMsg(code.DBQueryError, s.Lang)+" "+err.Error()))
	} else {
		c.JSON(http.StatusOK, model.OkData(getDate, code.GetMsg(code.DBQuerySuccess, s.Lang)))
	}
}
